cap program drop gitee
program define gitee
	version 14 
	syntax anything, [replace force From(string)]
    
	gettoken subc 0:0, p(" ")
	
	tokenize `"`0'"', p(",")
	local rnew `3'
	local webs `1'
	local webs=subinstr(`"`webs'"',"\","/",.) 
	
	gettoken w 1: 1, p("/\")
	 
	while `"`w'"'!=""{

		if !(`"`w'"'=="/" | `"`w'"'=="\"){
			
			local pkg `w'
			
		}
		
		gettoken w 1 : 1, p("/\")
	
	}
	
	

	tokenize `"`webs'"', p("/")
	local usr `1'`2'`3'
	local pth=subinstr(`"`webs'"',`"`usr'"',"",1)
	local pth=regexr(`"`pth'"',"/[^/]+$","")
	
	
	if (`"`subc'"'=="install" ) {
	
	  if `"`from'"'!=""{	
		
		local rnew=subinstr("`rnew'",`"from(`from')"',"",.)
		
        tempname N
        mata: files=cat(`"`from'"')
		mata: flag=select(1::length(files),strmatch(files,`"<i class="iconfont icon-file"></i>"'))
		mata: mata: st_numscalar("`N'",length(flag))
		
		if `=`N''==0{
			di as red "No Stata files found in the repository."
			exit
		}			
		
		mata: files=files[flag:+1,.]
		mata: files=subinstr(files,`"<a href=""',"https://gitee.com/",1)
		mata: files=subinstr(files,"</a>","",.)
		mata: urls=substr(files,1,strpos(files,`"""'):-1)
        mata: filenames=substr(files,strpos(files,`">"'):+1,.)
		mata: st_local("url",urls[1])
		local url=subinstr(`"`url'"',"/blob/","/raw/",1)
		local url=regexr(`"`url'"',"/[^/]+$","")
		
		mata: flag=(filenames:=="stata.toc")+(filenames:=="Stata.toc")
		mata: st_numscalar("`N'",sum(flag))
	
		if `=`N''==0{
			di as red "stata.toc is not found."
			di as red "There are not installable packages in the repository."
			exit
		}			
	

		mata: flag=strpos(filenames,"`pkg'.pkg")
		mata: st_numscalar("`N'",sum(flag))	
		
		if `=`N''==0{
			di as red "`pkg'.pkg is not found."
			di as red "Check the name of the installed package in the repository."
			mata: notation(filenames)
			exit
		}		
		
		
		
		net install `pkg', from(`url') `rnew'
		
		
	  }
	  else{
	     net install `pkg', from(https://gitee.com/`usr'/raw/master`pth') `rnew'	
	  }
	}
	
	else if (`"`subc'"'=="uninstall"){
	
	 ado uninstall `pkg'
	
	}
	else{
	  di as red "gitee should be followed by install or uninstall."
	  error 198
	
	}
	
end
		
cap mata mata drop notation()
cap mata mata drop strconcat()		
mata:

void function notation(string colvector filenames)

{
			flag=strpos(filenames,".pkg")
				if(sum(flag)>0){
				  st_global("spkg",strconcat(select(filenames,flag:>0)))
				  stata(`" di as red "Note: the specified repository includes $spkg " "')
				}
			


}

string function strconcat(string vector s)
{
   ss=""
   for(i=1;i<=length(s);i++){
   
	ss=ss+" " + s[i]
   
   }
   return(ss)


}


end

/*
cap program drop gitee
program define gitee
	version 13 
	syntax anything, [replace force from(string)]
    
	gettoken subc 0:0, p(" ")
	
	tokenize `"`0'"', p(",")
	local rnew `3'
	local webs `1'
	local webs=subinstr(`"`webs'"',"\","/",.) 
	
	gettoken w 1: 1, p("/\")
	 
	while `"`w'"'!=""{

		if !(`"`w'"'=="/" | `"`w'"'=="\"){
			
			local pkg `w'
			
		}
		
		gettoken w 1 : 1, p("/\")
	
	}
	
	

	tokenize `"`webs'"', p("/")
	local usr `1'`2'`3'
	local pth=subinstr(`"`webs'"',`"`usr'"',"",1)
	local pth=regexr(`"`pth'"',"/[^/]+$","")
	
	
	if (`"`subc'"'=="install" ) {
	
	  if `"`from'"'!=""{	
		
		local rnew=substr("`rnew'",1,strpos("`rnew'","from")-1)
		
        tempname N
        mata: files=cat(`"`from'"')
		mata: flag=select(1::length(files),strmatch(files,`"<i class="iconfont icon-file"></i>"'))
		mata: files=files[flag:+1,.]
		mata: files=subinstr(files,`"<a href=""',"https://gitee.com/",1)
		mata: files=subinstr(files,"</a>","",.)
		mata: urls=substr(files,1,strpos(files,`"""'):-1)
        mata: filenames=substr(files,strpos(files,`">"'):+1,.)
		mata: st_numscalar("`N'",length(files))
		//disp `N'
		
		preserve
		
		clear
		getmata urls=urls filenames=filenames
		
		qui gen flag=strpos(filenames,"`pkg'.pkg")
		su flag,meanonly
		
		local rmax=r(max)
		
		if `rmax'==0{
			di as red "`pkg'.pkg is not found."
			di as red "Check the name of the installed package in the repository."
            restore
			exit
		}
		
		qui replace flag=strpos(filenames,"stata.toc")+strpos(filenames,"Stata.toc")
		su flag,meanonly
		local rmax=r(max)
		
		if `rmax'==0{
			di as red "stata.toc is not found."
			di as red "There are not installable packages in the repository."
            restore
			exit
		}		
		mkdir _temp_`pkg'_
		forv j=1/`=`N''{
		    local web=urls[`j']
			local fn=filenames[`j']
			qui copy `web' ./_temp_`pkg'_/`fn' 
		}
		
		restore
		
		net install `pkg', from(`c(pwd)'/`temp_files') `rnew'
		
		//rmdir _temp_`pkg'_
		
	  }
	  else{
	     net install `pkg', from(https://gitee.com/`usr'/raw/master`pth') `rnew'	
	  }
	}
	
	else if (`"`subc'"'=="uninstall"){
	
	 ado uninstall `pkg'
	
	}
	else{
	  di as red "gitee should be followed by install or uninstall."
	  error 198
	
	}
	
end
		
*/		
		
		